package org.jeecg.modules.fund.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.fund.entity.FundCalendarCn;
import org.jeecg.modules.fund.mapper.FundCalendarCnMapper;
import org.jeecg.modules.fund.service.IFundCalendarCnService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class FundCalendarCnServiceImpl extends ServiceImpl<FundCalendarCnMapper, FundCalendarCn> implements IFundCalendarCnService {
    @Override
    public void createDate() {
        List<String> years = new ArrayList<>();
        years.add("2024");
        years.add("2025");
        years.add("2026");
        years.add("2027");
        years.add("2028");
        years.add("2029");
        years.add("2030");
        for (String year : years) {
            // 是否闰年
            boolean isLeap = DateUtil.isLeapYear(Integer.parseInt(year));
            // 一年总的多少天
            int days = isLeap ? 366 : 365;
            // 数组
            List<FundCalendarCn> fundCalendarCnList = new ArrayList<>();
            // 年份起始日期
            Date startDate = DateUtil.parseDate(year + "-01-01");
            for (int i = 0; i < days; i++) {
                // 偏离的日期
                Date date = DateUtil.offsetDay(startDate, i);
                // 构造日期
                FundCalendarCn fundCalendarCn = new FundCalendarCn()
                        .setId(DateUtil.format(date,"yyyyMMdd"))
                        .setDateName(date)
                        .setYear(year)
                        .setWeekName(DateUtil.dayOfWeekEnum(date).toChinese())
                        .setIsOpen(DateUtil.isWeekend(date) ? 1 : 0)
                        .setRemark(DateUtil.isWeekend(date) ? "周末休市" : null);
                fundCalendarCnList.add(fundCalendarCn);
            }
            this.saveBatch(fundCalendarCnList);
        }
    }
}
